home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: Float calculations
- Date: 2 Feb 1996 17:24:38 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4eu2v6$bhj@umbc9.umbc.edu>
- References: <4eqssf$d9q@camelot.ccs.neu.edu>
- NNTP-Posting-Host: umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- Jason Leatherman <jason@ccs.neu.edu> wrote:
- |> Check out the results I get when running this simple program on a
- |> Sparc/UNIX system, compiled with gcc:
- |>
- |> #include <stdio.h>
- |>
- |> void main()
-
- That alone is technically enough to produce undefined behavior in your
- program...Get rid of it! The main() function *HAS* to return an int.
-
- |> {
- |> float a, b;
-
- Change this to double...
-
- |> printf("%0.10f %0.10f %0.10f\n", 99974.0, 50.0, 99974.0/50.0);
- |>
- |> a = 99974.0;
- |> b = 50.0;
- |> printf("%0.10f %0.10f %0.10f\n", a, b, a/b);
-
- Add 'return (0);'...
- |> }
- |>
- |> The output is:
- |> 99974.0000000000 50.0000000000 1999.4800000000
- |> 99974.0000000000 50.0000000000 1999.4799804688
- |>
- |> Why do the divisions produce different results? This is probably some
- |> simple thing that I've forgotten, but I haven't figured it out yet. Does
- |> anyone know? Note that compiling with the -ffloat-store flag didn't make
- |> a difference.
-
- Using doubles in one printf() and floats in the other resulted in a slight
- loss of accuracy. Using a floating point constant in printf will be treated
- as a double. Since a and b are float they get evaluated as such in the
- division.
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-